package com.muyu.danmo.service.impl;

import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.DateUtils;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.controller.DanmoInformationController;
import com.muyu.danmo.domain.DanmoInformation;
import com.muyu.danmo.domain.DanmoPersonalPictures;
import com.muyu.danmo.domain.DanmoPortfolio;
import com.muyu.danmo.mapper.DanmoPortfolioMapper;
import com.muyu.danmo.req.DanmoPersonalPicturesReq;
import com.muyu.danmo.req.DanmoPortfolioReq;
import com.muyu.danmo.service.IDanmoPortfolioService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 作品集Service业务层处理
 *
 * @author yangle
 * @date 2024-09-21
 */
@Service
public class DanmoPortfolioServiceImpl implements IDanmoPortfolioService
{
    @Autowired
    private DanmoPortfolioMapper danmoPortfolioMapper;

    /**
     * 查询作品集
     *
     * @param id 作品集主键
     * @return 作品集
     */
    @Override
    public Result selectDanmoPortfolioById(Long id)
    {
        DanmoPortfolio danmoPortfolio = danmoPortfolioMapper.selectDanmoPortfolioById(id);
        if (danmoPortfolio == null){
            return Result.error("作品集为空");
        }
        danmoPortfolio.setViews(danmoPortfolio.getViews() + 1);
        danmoPortfolioMapper.updateViews(danmoPortfolio);
        return Result.success(danmoPortfolio);
    }

    /**
     * 查询作品集列表
     *
     * @param danmoPortfolio 作品集
     * @return 作品集
     */
    @Override
    public List<DanmoPortfolio> selectDanmoPortfolioList(DanmoPortfolio danmoPortfolio)
    {
        return danmoPortfolioMapper.selectDanmoPortfolioList(danmoPortfolio);
    }

    @Autowired
    private DanmoInformationServiceImpl danmoInformationService;

    @Autowired
    private DanmoInformationController danmoInformationController;
    /**
     * 新增作品集
     *
     * @param danmoPortfolio 作品集
     * @return 结果
     */
    @Transactional
    @Override
    public Result insertDanmoPortfolio(DanmoPortfolio danmoPortfolio)
    {



        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.warn("请登录");
        }

        Result result = validateDanmoTrainingInformation(danmoPortfolio);
        if (result != null) {
            return result;
        }
//        Result info = danmoInformationController.getInfo();
//        if (danmoInformation == null){
//            return Result.error("请先添加个人信息");
//        }
//        danmoPortfolio.setAuthor(danmoInformation.getName());
       DanmoInformation danmoInformation= danmoInformationService.selectUserID(loginUser.getUserid());
        if (danmoInformation == null){
            return Result.error("请先添加个人信息");
        }
        danmoPortfolio.setInformationId(danmoInformation.getId());
          danmoPortfolio.setAuthor(danmoInformation.getName());
        danmoPortfolio.setCreateTime(DateUtils.getNowDate());
        danmoPortfolio.setUpdateBy(loginUser.getUsername());
        danmoPortfolio.setCreateBy(loginUser.getUsername());
        danmoPortfolio.setUpdateTime(DateUtils.getNowDate());
        danmoPortfolio.setWorkStatus("展示中");
        danmoPortfolio.setViews(Long.valueOf(0));
        danmoPortfolioMapper.insertDanmoPortfolio(danmoPortfolio);
        insertDanmoPersonalPictures(danmoPortfolio);
        return Result.success(danmoPortfolio,"添加成功");
    }

    /**
     * 修改作品集
     *
     * @param danmoPortfolio 作品集
     * @return 结果
     */
    @Transactional
    @Override
    public Result updateDanmoPortfolio(DanmoPortfolio danmoPortfolio)
    {
        try{
            LoginUser loginUser = SecurityUtils.getLoginUser();
            if (loginUser == null) {
                return Result.warn("请登录");
            }

            Result result = validateDanmoTrainingInformation(danmoPortfolio);
            if (result != null) {
                return result;
            }
            DanmoInformation danmoInformation= danmoInformationService.selectUserID(loginUser.getUserid());
            danmoPortfolio.setInformationId(danmoInformation.getId());
            danmoPortfolio.setAuthor(danmoInformation.getName());
            danmoPortfolio.setUpdateTime(DateUtils.getNowDate());
            danmoPortfolio.setUpdateBy(loginUser.getUsername());
            danmoPortfolioMapper.deleteDanmoPersonalPicturesByPortfolioId(danmoPortfolio.getId());
            insertDanmoPersonalPictures(danmoPortfolio);
            danmoPortfolioMapper.updateDanmoPortfolio(danmoPortfolio);
            return Result.success(danmoPortfolio,"修改成功");
        }catch (Exception e) {
            return Result.error("修改失败");
        }
    }

    /**
     * 批量删除作品集
     *
     * @param ids 需要删除的作品集主键
     * @return 结果
     */
    @Transactional
    @Override
    public Result deleteDanmoPortfolioByIds(Long[] ids)
    {
        try {
            danmoPortfolioMapper.deleteDanmoPersonalPicturesByPortfolioIds(ids);
            return Result.success(danmoPortfolioMapper.deleteDanmoPortfolioByIds(ids),"删除成功");
        }catch (Exception e){
            return Result.error("删除失败");
        }

    }

    /**
     * 删除作品集信息
     *
     * @param id 作品集主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDanmoPortfolioById(Long id)
    {
        danmoPortfolioMapper.deleteDanmoPersonalPicturesByPortfolioId(id);
        return danmoPortfolioMapper.deleteDanmoPortfolioById(id);
    }

    @Override
    public List<DanmoPortfolio> selectInformationPortfolio(DanmoPortfolioReq danmoPortfolioReq) {

       return danmoPortfolioMapper.selectInformationPortfolio(danmoPortfolioReq);

    }

    /**
     * 查询个人信息作品图片集信息
     * @param danmoPersonalPicturesReq
     * @return
     */
    @Override
    public List<DanmoPersonalPictures> selectDanmoPersonalPictures(DanmoPersonalPicturesReq danmoPersonalPicturesReq) {
        return danmoPortfolioMapper.selectDanmoPersonalPictures(danmoPersonalPicturesReq);
    }



    /**
     * 新增个人信息作品图片集信息
     *
     * @param danmoPortfolio 作品集对象
     */
    public void insertDanmoPersonalPictures(DanmoPortfolio danmoPortfolio)
    {
        List<DanmoPersonalPictures> danmoPersonalPicturesList = danmoPortfolio.getDanmoPersonalPicturesList();
        Long id = danmoPortfolio.getId();
        if (StringUtils.isNotNull(danmoPersonalPicturesList))
        {
            List<DanmoPersonalPictures> list = new ArrayList<DanmoPersonalPictures>();
            for (DanmoPersonalPictures danmoPersonalPictures : danmoPersonalPicturesList)
            {
                danmoPersonalPictures.setInformationId(id);
                list.add(danmoPersonalPictures);
            }
            if (list.size() > 0)
            {
                danmoPortfolioMapper.batchDanmoPersonalPictures(list);
            }
        }
    }

    @Override
    public Result updateDanmoCollectShellfishById(Long id) {
        DanmoPortfolio danmoPortfolio = danmoPortfolioMapper.selectDanmoCollectShellfishById(id);
        if (danmoPortfolio.getWorkStatus().equals("已下架")) {
            return Result.error("下架失败");
        }
        danmoPortfolio.setWorkStatus("已下架");
        danmoPortfolioMapper.updateDanmoCollectShellfish(danmoPortfolio);
        return Result.success(id,"下架成功");
    }

    @Override
    public Result updateDanmoCollectShellfishListedById(Long id) {
        DanmoPortfolio danmoPortfolio = danmoPortfolioMapper.selectDanmoCollectShellfishById(id);
        if (danmoPortfolio.getWorkStatus().equals("展示中")) {
            return Result.error("上架失败");
        }
        danmoPortfolio.setWorkStatus("展示中");
        danmoPortfolioMapper.updateDanmoCollectShellfish(danmoPortfolio);
        return Result.success(id,"上架成功");
    }

    @Override
    public List<DanmoPortfolio> selectInformationPortfolioByOthers(DanmoPortfolioReq danmoPortfolioReq) {

        return danmoPortfolioMapper.selectInformationPortfolioByOthers(danmoPortfolioReq);
    }

    private Result validateDanmoTrainingInformation(DanmoPortfolio danmoPortfolio) {
        if (danmoPortfolio == null) {
            return Result.error("信息不能为空");
        }
        if (danmoPortfolio.getName() == null || danmoPortfolio.getName().isEmpty()){
            return Result.error("请输入作品名称");
        }
        return null;
    }


}
